import datetime
from django.contrib import admin
from django.db import models
from django.utils import timezone


class Question(models.Model):
    question_text = models.CharField('问题内容', max_length=200)
    pub_date = models.DateTimeField('发布时间')
    end_date = models.DateTimeField('截止时间', null=True, blank=True)
    def __str__(self):
        return self.question_text
    @admin.display(
        boolean=True,
        ordering="pub_date",
        description="最近发布？",
    )
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

    class Meta:
        verbose_name = '投票问题'
        verbose_name_plural = '投票问题'


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE, verbose_name='所属问题')
    choice_text = models.CharField('选项内容', max_length=200)
    votes = models.IntegerField('票数', default=0)
    def __str__(self):
        return self.choice_text

    class Meta:
        verbose_name = '投票选项'
        verbose_name_plural = '投票选项'
